<template>
  <div>
    <div><h1>SD单据信息</h1></div>
    <div>
      <div>
        <el-form :inline="true" :model="formsap" :label-position="right" label-width="120px">
          <el-form-item label="sap单号:">
            <el-input v-model="formsap.sapNumber" class="saptext" type="text"></el-input>
            <el-button type="primary" @click="selectsap" icon="el-icon-searchsousuo">搜索</el-button>
          </el-form-item>
          <el-form-item label="物料合计低值:">
            <el-input class="jhtext" v-model="date.DDWLDZ" type="text"/>
          </el-form-item>
          <el-form-item label="物料合计高值:">
            <el-input class="jhtext" v-model="date.DDWLGZ" type="text"/>
          </el-form-item>
        </el-form>

      </div>
      <div>
        <h2>H表头</h2>
      </div>
      <div class="form-container">
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="交货:">
            <el-input class="jhtext" v-model="date.cg_h.VBELN" type="text"/>
          </el-form-item>
          <el-form-item label="交货类型:">
            <el-input class="jhlxtext" v-model="date.cg_h.LFART" type="text"/>
          </el-form-item>
          <el-form-item label="客户编号:">
            <el-input class="jhrqtext" v-model="date.cg_h.KUNNR" type="text"/>
          </el-form-item>
          <el-form-item label="送达方名称:">
            <el-input class="gystext" v-model="date.cg_h.NAME_SH" type="text"/>
          </el-form-item>
          <el-form-item label="售达方:">
            <el-input class="gysmctext" v-model="date.cg_h.KUNAG" type="text"/>
          </el-form-item>
          <el-form-item label="售达方名称:">
            <el-input class="sjxmtext" v-model="date.cg_h.NAME_SD" type="text"/>
          </el-form-item>
          <el-form-item label="司机姓名:">
            <el-input class="chtext" v-model="date.cg_h.ZSJXM" type="text"/>
          </el-form-item>
          <el-form-item label="车号:">
            <el-input class="chtext" v-model="date.cg_h.ZCHEH" type="text"/>
          </el-form-item>
          <el-form-item label="司机电话:">
            <el-input class="chtext" v-model="date.cg_h.ZSJDH" type="text"/>
          </el-form-item>
          <el-form-item label="标识退货交货单:">
            <el-input class="chtext" v-model="date.cg_h.ZFLAG" type="text"/>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>I表头</h2></div>
      <div>
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="交货">
            <el-input class="jhtext" v-model="date.cg_i.VBELN" type="Text"/>
          </el-form-item>
          <el-form-item label="交货项目">
            <el-input class="jhxmtext" v-model="date.cg_i.POSNR" type="Text"/>
          </el-form-item>
          <el-form-item label="物料编号">
            <el-input class="wlhtext" v-model="date.cg_i.MATNR" type="Text"/>
          </el-form-item>
          <el-form-item label="物料描述">
            <el-input class="wlmstext" v-model="date.cg_i.MAKTX" type="Text"/>
          </el-form-item>
          <el-form-item label="工厂">
            <el-input class="gctext" v-model="date.cg_i.WERKS" type="Text"/>
          </el-form-item>
          <el-form-item label="库存地点">
            <el-input class="gcmctext" v-model="date.cg_i.LGORT" type="Text"/>
          </el-form-item>
          <el-form-item label="库存地点的描述">
            <el-input class="kcddtext" v-model="date.cg_i.LGOBE" type="Text"/>
          </el-form-item>
          <el-form-item label="实际已交货量">
            <el-input class="kcmctext" v-model="date.cg_i.LFIMG" type="Text"/>
          </el-form-item>
          <el-form-item label="销售单位">
            <el-input class="jhsltext" v-model="date.cg_i.VRKME" type="Text"/>
          </el-form-item>
          <el-form-item label="件数">
            <el-input class="jbjldwtext" v-model="date.cg_i.ZJS" type="Text"/>
          </el-form-item>
          <el-form-item label="包装物">
            <el-select class="ymhttext" v-model="date.cg_i.BEZEI" placeholder="请选择">
              <el-option
                v-for="item in ymhthOptions"
                :key="item.id"
                :label="item.dm"
                :value="item.dm">
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div><h2>备注信息</h2></div>
      <div>
        <el-form :inline="true" v-model="date" :label-position="right" label-width="140px">
          <el-form-item label="物流名">
            <el-input class="chehao" v-model="date.wlm" type="Text"/>
          </el-form-item>
          <el-form-item label="司机身份证">
            <el-input class="chehao" v-model="date.sjsfz" type="Text"/>
          </el-form-item>
          <el-form-item label="内外销">
            <el-input class="sjsfz" v-model="date.nwx" type="Text"/>
          </el-form-item>
          <el-form-item label="原始单号">
            <el-input class="ddwlsl" v-model="date.ysdh" type="Text"/>
          </el-form-item>
          <el-form-item label="原始重量">
            <el-input class="ysdh" v-model="date.yszl" type="Text"/>
          </el-form-item>
          <el-form-item label="原发日期">
            <el-input class="yszl" v-model="date.yfrq" type="Text"/>
          </el-form-item>
          <el-form-item label="原发矿">
            <el-input class="yfrq" v-model="date.yfk" type="Text"/>
          </el-form-item>
          <el-form-item label="规格">
            <el-input class="guige" v-model="date.guige" type="Text"/>
          </el-form-item>
          <el-form-item label="手机号">
            <el-input class="openid" v-model="date.openid" type="Text"/>
          </el-form-item>

          <el-form-item label="超长车标记:">
            <el-checkbox
              v-model="date.yc_ccc"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>

          </el-form-item>
          <el-form-item label="小于200kg出场:">
            <el-checkbox
              v-model="date.xy200kg"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>
          </el-form-item>
          <el-form-item label="一单多物料:">
            <el-checkbox
              v-model="date.dwl"
              :true-label='1'
              :false-label='0'
            >
            </el-checkbox>
          </el-form-item>
        </el-form>
      </div>
      <div>
        <el-button type="primary" @click="submitData">提交</el-button>
      </div>
    </div>
  </div>
</template>

<script>

import {
  countWlccfw, insertbkdp,
  lisTb_chengys,
  lisTb_hetonghuayan,
  selectCgh, selectCgi, selectSdh, selectSdI, selectWlccfwdm,
  tbGuozhongExists,
  tbGuozhonginsert, tbGuozhonginsertsd
} from "@/api/system/Fdongbei/zd";


export default {
  data() {
    return {
      right: "",
      formsap: {
        sapNumber: '' // 初始化表单数据
      },
      date: {
        cg_h: {
          VBELN: '',
          KUNNR: '',
          NAME_SH: '',
          KUNAG: '',
          NAME_SD: '',
          ZSJXM: '',
          ZCHEH: '',
          ZSJDH: '',
        },
        cg_i: {
          VBELN: '',
          POSNR: '',
          MATNR: '',
          MAKTX: '',
          WERKS: '',
          LGORT: '',
          LGOBE: '',
          LFIMG: '',
          VRKME: '',
          BEZEI: '',
          ZJS: '',
        },
        DDWLDZ: '',
        DDWLGZ: '',
        wlm: '',
        sjsfz: '',
        nwx: '',
        ysdh: '',
        yszl: '',
        yfrq: '',
        yfk: '',
        guige: '',
        openid: '',
        yc_ccc: '',
        xy200kg: '',
        dwl: ''
      },

      wl: {
        nxWx: '',
        wlbh: '',
      },
      ymhthOptions: [],
      pd: "",
      guozhong: {
        wlmcc: '',
        jsdj: '',
        card_id: '',
        chehao: '',
        czbh: '',
        wlbh: '',
        wlmc: '',
        gblx: '',
        gysbh: '',
        gysdm: '',
        ckbh: '',
        ckmc: '',
        dbh: '',
        gsbh: '',
        ddxmbh: '',
        gbzt: '',
        sjxm: '',
        sjdh: '',
        sjsfz: '',
        yzkz: '',
        ddwlsl: '',
        ddysdh: '',
        ddyszl: '',
        nlmtbz1: '',
        nlmtbz2: '',
        czymc: '',
        sapbzsc: '',
        yfrq: '',
        yfk: '',
        ddbh: '',
        ymqysj: '',
        guige: '',
        songdf: '',
        sdfbh: '',
        shoudf: '',
        khbh: '',
        jhxm: '',
        fkZdr: '',
        sdDwx: '',
        sdBzwmc: '',
        kkRjbb: '',
        ycCcc: '',
        fksj: '',
        htYcclYcKg: '',
      }
    }
      ;
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
    },
    selectsap() {


      try {
        selectSdh({VBELN: this.formsap.sapNumber}).then(response => {
          if (response.data === undefined) {
            this.$modal.alertWarning("Tb_cg_h缺失")
          }
          const data = response.data;
          this.date.cg_h = {
            //  NAME_SH:data.name_sh,
            LFART: data.lfart,
            VBELN: data.vbeln,
            KUNNR: data.kunnr,
            NAME_SH: data.name_Sh,
            KUNAG: data.kunag,
            NAME_SD: data.name_Sd,
            ZSJXM: data.zsjxm,
            ZCHEH: data.zcheh,
            ZSJDH: data.zsjdh,
          };
          if (data.LFART === "YELR" || data.LFART === "YFC5" || data.LFART === "YLC1" || data.LFART === "YLC5" || data.LFART === "YLF0" || data.LFART === "YLF1" || data.LFART === "YLF8" || data.LFART === "YLF9") {
            this.date.nwx = "外销";
          } else {
            this.date.nwx = "内销";
          }
        });
      } catch (error) {
        console.error('请求失败:', error);
      }


      try {
        selectSdI({VBELN: this.formsap.sapNumber}).then(response => {
          if (response.data === undefined) {
            this.$modal.alertWarning("Tb_cg_i缺失")
          }
          const data = response.data;
          this.date.cg_i = {
            LFIMG: data.lfimg,
            VRKME: data.vrkme,
            ZJS: data.zjs,
            BEZEI: data.bezei,
            VBELN: data.vbeln,
            POSNR: data.posnr,
            EBELN: data.ebeln,
            EBELP: data.ebelp,
            MATNR: data.matnr,
            MAKTX: data.maktx,
            WERKS: data.werks,
            NAME1: data.name1,
            LGORT: data.lgort,
            LGOBE: data.lgobe,
            MENGE: data.menge,
            MEINS: data.meins,
            NETPR: data.netpr,

          };
          this.wl = {
            nxWx: this.date.nwx,
            wlbh: this.date.cg_i.MATNR,
          }
          selectWlccfwdm(this.wl).then(response => {
            this.ymhthOptions = response.data
            console.log(response.data)
          })
        });
      } catch (error) {
        console.error('请求失败:', error);
      }


    },
    submitData() {//提交


      if (this.date.cg_h.ZCHEH === "") {
        this.qk()
        this.$modal.alertWarning("车号不能为空")
        return;
      }
      countWlccfw({nx_wx: this.date.nwx, wlbh: this.date.cg_i.MATNR, dm: this.date.cg_i.BEZEI}).then(response => {
        console.log(response.data)
        if (response.msg === 0) {
          this.$modal.alertWarning('缺少袋皮标准')

        } else {

          tbGuozhongExists({chehao: this.date.cg_h.ZCHEH}).then(response => {
            if (response.msg === 'OK') {
              this.guozhong = {
                wlmcc: this.date.wlm,
                jsdj: '0.00',
                card_id: '',
                chehao: this.date.cg_h.ZCHEH,
                czbh: 'DC',
                wlbh: this.date.cg_i.MATNR,
                wlmc: this.date.cg_i.MAKTX,
                gblx: 'YLF',
                gysbh: '',
                gysdm: this.date.cg_h.NAME_SD,
                ckbh: this.date.cg_i.LGORT,
                ckmc: this.date.cg_i.LGOBE,
                dbh: '',
                gsbh: this.date.cg_i.WERKS,
                ddxmbh: this.date.cg_i.POSNR,
                gbzt: '待进厂',
                sjxm: this.date.cg_h.ZSJXM,
                sjdh: this.date.cg_h.ZSJDH,
                sjsfz: this.date.sjsfz,
                yzkz: '0.00',
                ddwlsl: this.date.cg_i.LFIMG,
                ddysdh: this.date.ysdh,
                ddyszl: this.date.yszl,
                nlmtbz1: '',
                nlmtbz2: '',
                czymc: this.$store.state.user.name,
                sapbzsc: this.date.cg_h.ZFLAG,
                yfrq: this.date.yfrq,
                yfk: this.date.yfk,
                ddbh: this.formsap.sapNumber,
                ymqysj: '',
                guige: this.date.guige,
                songdf: this.date.cg_h.NAME_SH,
                sdfbh: this.date.cg_h.KUNAG,
                shoudf: this.date.cg_h.NAME_SD,
                khbh: this.date.cg_h.KUNNR,
                jhxm: this.date.cg_i.POSNR,
                fkZdr: this.$store.state.user.name,
                sdDwx: this.date.nwx,
                sdBzwmc: this.date.cg_i.BEZEI,
                kkRjbb: '',
                ycCcc: this.date.yc_ccc,
                fksj: new Date().toISOString(),
                htYcclYcKg: this.date.xy200kg,
              }
              tbGuozhonginsertsd(this.guozhong).then(response => {
                if (response.msg !== 0) {
                  this.qk()

                  this.$modal.alertWarningd("保存成功")

                } else {
                  this.qk()
                  this.$modal.alertWarning("保存失败")

                }
              })
            } else {
              this.qk()
              this.$modal.alertWarning(response.msg)
            }
          })
        }
      })


    },
    message(mag) {
      this.$alert(mag, "消息弹框", {
        confirmButtonText: '确定',
        callback: action => {
          this.$message({
            type: 'info',
            message: `action: ${action}`
          });
        }
      });
    },
    qk() {
      this.formsap = {
        sapNumber: '' // 初始化表单数据
      },
        this.date = {
          cg_h: {
            VBELN: '',
            KUNNR: '',
            NAME_SH: '',
            KUNAG: '',
            NAME_SD: '',
            ZSJXM: '',
            ZCHEH: '',
            ZSJDH: '',
          },
          cg_i :{
            VBELN: '',
            POSNR: '',
            MATNR: '',
            MAKTX: '',
            WERKS: '',
            LGORT: '',
            LGOBE: '',
            LFIMG: '',
            VRKME: '',
            BEZEI: '',
            ZJS: '',
          },
          DDWLDZ: '',
          DDWLGZ: '',
          wlm: '',
          sjsfz: '',
          nwx: '',
          ysdh: '',
          yszl: '',
          yfrq: '',
          yfk: '',
          guige: '',
          openid: '',
          yc_ccc: '',
          xy200kg: '',
          dwl: ''
        },
        this.wl = {
          nxWx: '',
          wlbh: '',
        },
        this.ymhthOptions = [],
        this.pd = "",
        this.guozhong
          =
          {
            wlmcc: '',
            jsdj: '',
            card_id: '',
            chehao: '',
            czbh: '',
            wlbh: '',
            wlmc: '',
            gblx: '',
            gysbh: '',
            gysdm: '',
            ckbh: '',
            ckmc: '',
            dbh: '',
            gsbh: '',
            ddxmbh: '',
            gbzt: '',
            sjxm: '',
            sjdh: '',
            sjsfz: '',
            yzkz: '',
            ddwlsl: '',
            ddysdh: '',
            ddyszl: '',
            nlmtbz1: '',
            nlmtbz2: '',
            czymc: '',
            sapbzsc: '',
            yfrq: '',
            yfk: '',
            ddbh: '',
            ymqysj: '',
            guige: '',
            songdf: '',
            sdfbh: '',
            shoudf: '',
            khbh: '',
            jhxm: '',
            fkZdr: '',
            sdDwx: '',
            sdBzwmc: '',
            kkRjbb: '',
            ycCcc: '',
            fksj: '',
            htYcclYcKg: '',
          }
    }
  }
};

</script>

<style scoped lang="scss">
.el-icon-searchsousuo {
  display: flex;
  align-items: center;
}

.el-input {
  width: auto;
  flex-grow: 3;
}

.el-button {
  margin-left: 10px; /* 调整按钮与输入框之间的间距 */
  flex-shrink: 0; /* 防止按钮在宽度不足时被压缩 */
}

.input-with-select {
  background-color: #fff;
}
</style>
